// 获取元素属性
export const getElement = (selector) => {
  return new Promise((resolve, reject) => {
    uni
      .createSelectorQuery()
      .select(selector)
      .fields({
        style: true,
        node: true,
        size: true,
        scrollOffset: true,
      })
      .exec((res) => {
        if (res && res.length > 0) {
          resolve(res[0])
        } else {
          reject('查询结果为空')
        }
      })
    // uni
    //   .createSelectorQuery()
    //   .select(selector)
    //   .boundingClientRect()
    //   .selectViewport()
    //   .scrollOffset()
    //   .exec((res) => {
    //     if (res && res.length > 0) {
    //       console.log(res)
    //       resolve(res[0])
    //     } else {
    //       reject('查询结果为空')
    //     }
    //   })

    // wx.createSelectorQuery()
    // .select('.canvas-bg')
    // .node(function (res) {
    //   console.log(res.node) // 节点对应的 Canvas 实例。
    // })
    // .exec()
  })
}

// 高度计算
export const getYHeight = (len, _height, gridScale = 1, distribution = "between") => {
  if (!len) return false;
  let list = [];
  if (distribution === "between") {
    const eve = _height / (len + 1);
    for (let A = 1; A < len + 1; A++) {
      const eveHeight = (eve * A) / gridScale;
      list.push(eveHeight);
    }
  } else if (distribution === "around") {
    const eve = _height / (len * 2);
    let eveHeight = 0;
    for (let A = 1; A < len + 1; A++) {
      if (A > 1) {
        eveHeight = (eve * A + eve * (A - 1)) / gridScale;
      } else {
        eveHeight = (eve * A) / gridScale;
      }
      list.push(eveHeight);
    }
  }
  return list;
};

// 获取导联数组值下标
export const getLeadList = (inputLen, beginIndex = 0, endIndex = inputLen) => {
  if (!inputLen) return [];
  let list = [];
  for (let index = 0; index < inputLen; index++) list.push(index);
  // 处理负数索引的情况
  beginIndex = beginIndex < 0 ? list.length + beginIndex : beginIndex;
  endIndex = endIndex < 0 ? list.length + endIndex : endIndex;

  // 确保 beginIndex 不大于 endIndex
  if (beginIndex >= endIndex) {
    return Array.isArray(list) ? [] : "";
  }
  // 使用内置的 slice 方法来实现切片
  return list.slice(beginIndex, endIndex);
};

// 设置导联高度
const contain = ["V1", "V2", "V3", "V4", "V5", "V6", "X", "Y", "Z", "V3R", "V4R", "V5R", "V7", "V8", "V9"];
export const getGainValue = (gain, name) => {
  if (typeof gain === "string") {
    // 增益为 10/5mm/mV
    if (gain === "10/5") {
      return contain.includes(name) ? 5 : 10;
    }
    // 预留
    if (gain === "AGC") {
      return 10;
    }
  } else {
    return gain;
  }
};

// 设置/r/n换行
export const getHtml = text => {
  if (!text) return "";
};

// u将字符串转换成数组
export const convertDetailInfoToArray = detailInfo => {
  const result = {};
  for (const key in detailInfo) {
    if (detailInfo.hasOwnProperty(key)) {
      result[key] = detailInfo[key].split("/").filter(item => item !== "");
    }
  }
  return result;
};

// 获取 像素dpr（scale）
export const getScale = () => {
  const systemInfo = uni.getWindowInfo();
  const dpi = systemInfo.pixelRatio;
  return dpi
}
